//
//  RHSetViewController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/20.
//

#import "RHSetViewController.h"
#import "RHAccountViewController.h"
#import "RHAboutViewController.h"
#import "RHBaseWebViewController.h"
#import "RHSetViewCell.h"
#import "RHLoginViewController.h"
#import "RHNavigationController.h"
#import "RHLanguageSettingVC.h"

@interface RHSetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) UIView *headerView;

@property (nonatomic ,strong) UIView *footerView;

@end

@implementation RHSetViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"设置".rh_localized];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self setupUI];
}

- (void)setupUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHSetViewCell class] forCellReuseIdentifier:@"RHSetViewCell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
}


- (UIView *)footerView{
    if(!_footerView){
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,300)];
        [_footerView setBackgroundColor:UIColor.clearColor];
        
        UIView *view =  [[UIView alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30,200)];
        [view setBackgroundColor:UIColor.whiteColor];
        [view round:5 RectCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
        [_footerView addSubview:view];
        
        QMUIButton *logoutBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [logoutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [logoutBtn setTitle:@"退出登录".rh_localized forState:UIControlStateNormal];
        [logoutBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [logoutBtn.layer setCornerRadius:8];
        [logoutBtn setBackgroundColor:CMainColorGreen];
        [_footerView addSubview:logoutBtn];
#pragma mark ----- 退出登录 ------
        [[logoutBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [UserDefaults setBool:NO forKey:kIsLogined];
            [UserDefaults synchronize];
            RHLoginViewController *Login_vc = [[RHLoginViewController alloc] init];
            RHNavigationController *login_nav = [[RHNavigationController alloc] initWithRootViewController:Login_vc];
            [[UIApplication sharedApplication].keyWindow setRootViewController:login_nav];

        }];
        
        [logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.height.mas_equalTo(50);
            make.bottom.mas_offset(0);
        }];
    }
    return _footerView;
}

- (UIView *)headerView{
    if(!_headerView){
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,30)];
        [_headerView setBackgroundColor:UIColor.clearColor];
        
        UIView *view =  [[UIView alloc] initWithFrame:CGRectMake(15, 20, SCREEN_WIDTH-30,10)];
        [view setBackgroundColor:UIColor.whiteColor];
        [view round:5 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
        [_headerView addSubview:view];
    }
    return _headerView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 300;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
   return self.footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *lan = [kUserDefaults objectForKey:kLanguage];
    if([lan containsString:@"zh"]){
        lan = @"中文";
    }else if ([lan containsString:@"en"]){
        lan = @"English";
    }
    else if ([lan containsString:@"es"]){
        lan = @"Español";
    }
    else{
        lan = @"";
    }
    
    RHSetViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHSetViewCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSArray *titles = @[@"语言".rh_localized,@"账号".rh_localized,@"服务条款".rh_localized,@"隐私策略".rh_localized,@"关于".rh_localized];
    NSArray *contents = @[lan,[PrivateMange share].currentUser.email,@"",@"",@"V1.0.0"];
    [cell setTitleStr:titles[indexPath.row]];
    [cell setContentStr:contents[indexPath.row]];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row == 0){
        //语言
        RHLanguageSettingVC *lanVC = [[RHLanguageSettingVC alloc]init];
        [self.navigationController pushViewController:lanVC animated:YES];
    }
    
    if(indexPath.row == 1){
        //账号
        RHAccountViewController *vc = [[RHAccountViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 2){
        //用户协议
        RHBaseWebViewController *vc = [[RHBaseWebViewController alloc] init];
//        [vc setLocalHtml:@"terms_of_use"];
        vc.urlStr = @"https://www.ruihenergy.com/terms_of_use";
        vc.navTitle =  @"服务条款".rh_localized;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 3){
        //隐私协议
        RHBaseWebViewController *vc = [[RHBaseWebViewController alloc] init];
//        [vc setLocalHtml:@"privacy_policy"];
        vc.urlStr = @"https://www.ruihenergy.com/privacy_policy";
        vc.navTitle = @"隐私政策".rh_localized;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 4){
        //关于
        RHAboutViewController *vc = [[RHAboutViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}



@end
